﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace starLiGHT.GUI.Widgets
{
    public class CheckButton : Button, ICheckable
    {
        bool isChecked;


        public CheckButton(GuiManager manager, string name)
            : base(manager, name)
        {
        }

        public CheckButton(GuiManager manager, string name, string layout)
            : base(manager, name, layout)
        {
        }

        public event EventHandler CheckChanged;


        public bool Checked
        {
            get
            {
                return isChecked;
            }
            set
            {
                if (value != isChecked)
                {
                    isChecked = value;
                    SetState((value) ? "Pushed" : "Normal");
                    OnCheckChanged(this, new EventArgs());
                }
            }
        }

        public void OnCheckChanged(object sender, EventArgs e)
        {
            if (this.CheckChanged != null)
            {
                this.CheckChanged(sender, e);
            }
        }

        public override void OnMouseEnter(object sender, MouseEventArgs e)
        {
            base.OnMouseEnter(sender, e);
            SetPressed();
        }
        public override void OnMouseLeave(object sender, EventArgs e)
        {
            base.OnMouseLeave(sender, e);
            SetPressed();
        }
        public override void OnMouseUp(object sender, MouseEventArgs e)
        {
            base.OnMouseUp(sender, e);
            SetPressed();
        }

        public override void OnClick(object sender, EventArgs e)
        {
            base.OnClick(sender, e);

            this.Checked = !this.Checked;
        }

        private void SetPressed()
        {
            if (isChecked)
            {
                SetState("Pushed");
            }
        }
    }
}
